package com.journey.org.ui.custom.dialog

import android.app.Application
import android.content.Intent
import android.os.Bundle
import androidx.databinding.ObservableField
import com.journey.org.entity.MineAccountEntity
import com.journey.org.ui.web.WebActivity
import com.mvvmhabit.org.base.BaseViewModel
import com.mvvmhabit.org.binding.command.BindingAction
import com.mvvmhabit.org.binding.command.BindingCommand

class AccountDetailViewModel(application: Application) : BaseViewModel(application) {

    val onAccountDetailField = ObservableField<MineAccountEntity>()

    // 进入Web
    val onClickEnterWebCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val bundle = Bundle()
            bundle.putString("loadUrl", onAccountDetailField.get()?.accountAddress)
            bundle.putString("title", onAccountDetailField.get()?.name)
            startActivity(WebActivity::class.java, bundle)
        }
    })

    // 分享
    val onClickShareCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            share(onAccountDetailField.get()?.name!!, onAccountDetailField.get()?.accountAddress!!)
        }
    })

    private fun share(title: String, url: String) {
        var intentShare = Intent()
        intentShare.action = Intent.ACTION_SEND
        intentShare.type = "text/plain"
        intentShare.putExtra(Intent.EXTRA_SUBJECT, title)
        intentShare.putExtra(Intent.EXTRA_TEXT, url)
        intentShare = Intent.createChooser(intentShare, title)
        startActivity(intentShare)
    }
}